package com.fuhe.studydemo.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsoluteLayout;

public class OffsetLayout extends AbsoluteLayout {

    private int mOffsetHorizontal = 0;
    private int mOffsetVertical = 0;

    public OffsetLayout(Context context) {
        this(context,null);
    }

    public OffsetLayout(Context context, AttributeSet attrs) {
        super(context, attrs,0);
    }

    public OffsetLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        for (int i=0;i<getChildCount();i++){
            View child = getChildAt(i);
            int new_left = (r-l)/2-child.getMeasuredWidth()/2 +mOffsetHorizontal+100;
            int new_top = (b-t)/2-child.getMeasuredHeight()/2 +mOffsetVertical+100;
            child.layout(new_left,new_top,new_left+child.getMeasuredWidth(),new_top+child.getMeasuredHeight());
        }
    }

    public void setmOffsetHorizontal(int offset){
        mOffsetHorizontal = offset;
        mOffsetVertical = 0;
        requestLayout();
    }

    public void setmOffsetVertical(int offset){
        mOffsetHorizontal = 0;
        mOffsetVertical = offset;
        requestLayout();
    }
}
